import { Injectable } from '@angular/core';
import { Platform } from 'ionic-angular';
import { JmessageChenyu } from "jmessage-chenyu";
import { DialogProvider } from '../../providers/dialog/dialog';
/*
  Generated class for the JmessageProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class JmessageProvider {

  constructor(
    public platform: Platform,
    public jmessage: JmessageChenyu,
    public dialog: DialogProvider) {
    console.log('Hello JmessageProvider Provider');
  }

  public initJmessage() {
    // 初始化插件
    this.jmessage.init({ isOpenMessageRoaming: true });
  }

  //用户注册
  public regist(obj: any) {
    //{username:1,password:2,nickname:3,gender:'male',extras:{key1:'',key2:''}};
    if (this.platform.is('cordova')) {
      this.jmessage.register(obj).then(() => {
        this.dialog.showToast("top", "注册成功，请登录！");
      })
        .catch((error) => {
          this.dialog.showToast("top", "注册失败" + JSON.stringify(error))
        });
    } else {
      //执行web版本
    }
  }

  //用户登录
  public login(obj: any) {
    //{ username: 'username', password: 'password' }
    this.jmessage.login(obj).then(() => {

    })
      .catch((error) => {
        this.dialog.showToast("top", "登陆失败" + JSON.stringify(error))
      });
  }

  //注销登录
  public logout() {
    this.jmessage.logout().then(() => {

    })
      .catch((error) => {
        this.dialog.showToast("top", "注销失败" + JSON.stringify(error))
      });
  }
  //获取自己信息
  public getMyInfo() {
    this.jmessage.getMyInfo().then((myInfo) => {

    })
      .catch((error) => {
        this.dialog.showToast("top", "获取自己信息失败" + JSON.stringify(error))
      });
  }
  //获取他人信息
  public getUserInfo(obj: any) {
    //{ username: 'username', appKey: 'your_app_key' }
    this.jmessage.getUserInfo(obj).then((userInfo) => {

    })
      .catch((error) => {
        this.dialog.showToast("top", "获取他人信息失败" + JSON.stringify(error))
      });
  }
  //更新当前用户密码
  public updateMyPassword(obj: any) {
    //{ oldPwd: 'old_password', newPwd: 'new_password' }
    this.jmessage.updateMyPassword(obj).then(() => {

    })
      .catch((error) => {
        this.dialog.showToast("top", "更新当前用户密码失败" + JSON.stringify(error))
      });
  }
  //更新当前用户头像
  public updateMyAvatar(obj: any) {
    //{ imgPath: 'img_local_path' },
    this.jmessage.updateMyAvatar(obj).then(() => {

    })
      .catch((error) => {
        this.dialog.showToast("top", "更新当前用户头像失败" + JSON.stringify(error))
      });
  }
  //下载用户头像缩略图
  public downloadThumbUserAvatar(obj: any) {
    //{ username: 'username', appKey: 'target_user_appKey' }
    this.jmessage.downloadThumbUserAvatar(obj).then((result) => {
      //var username = result.username
      //var appKey = result.appKey
      //var filePath = result.filePath  // 图片文件路径
    })
      .catch((error) => {
        this.dialog.showToast("top", "下载用户头像缩略图失败" + JSON.stringify(error))
      });
  }
  //下载用户头像原图
  public downloadOriginalUserAvatar(obj: any) {
    //{ username: 'username', appKey: 'target_user_appKey' }
    this.jmessage.downloadOriginalUserAvatar(obj).then((result) => {
      //var username = result.username
      //var appKey = result.appKey
      //var filePath = result.filePath  // 图片文件路径
    })
      .catch((error) => {
        this.dialog.showToast("top", "下载用户头像原图失败" + JSON.stringify(error))
      });
  }
}
